import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class testwork1 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> ret=new LinkedList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            LinkedList<Integer> fist=new LinkedList<>();
            int size=queue.size();

            while(size!=0){
                size--;
                TreeNode cur=queue.poll();
                fist.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            ret.add(fist);
        }
        return ret;
    }
}
